/***************************************************************************
 *   Copyright (C) 2004 by Qball Cow	 		                   *
 *   e-mail: Qball@qballcow.nl 						   *
 *   Code is first based on work by Nicolas Sutre, 			   *
 *   but no original code is left                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef __UEIB_H__
#define __UEIB_H__

#include <usb.h>  /* Added by GJ 05feb2006 */

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif 

#ifndef MAX
#define MAX(a,b) (a > b)? a:b
#endif

#ifndef MIN
#define MIN(a,b) (a < b)? a:b
#endif

#define RANGE(a,b,c) (a<c)?MAX(a,b):MIN(a,c)

#define UEIB_INP1 16
#define UEIB_INP2 32
#define UEIB_INP3 1
#define UEIB_INP4 64
#define UEIB_INP5 -128

#define UEIB_OUT1 1
#define UEIB_OUT2 2
#define UEIB_OUT3 4
#define UEIB_OUT4 8
#define UEIB_OUT5 16
#define UEIB_OUT6 32
#define UEIB_OUT7 64
#define UEIB_OUT8 128

#define UEIB_ANA1 0
#define UEIB_ANA2 1				    

#define UEIB_COUNT1 0
#define UEIB_COUNT2 1				    
				    
#define UEIB_DEVICE1 0
#define UEIB_DEVICE2 1
#define UEIB_DEVICE3 2
#define UEIB_DEVICE4 3

					
typedef struct ueib_handle
{
	/* the device ID */
	int device_id;
	/* usb device handle */
	usb_dev_handle *device_handle;

	/* INPUTS */
	/* the digital 6 bit input value */
	int dig_input;
	/* the analoge input(s)*/
	int ana_input_1;
	int ana_input_2;
	/* internal counters */
	int count_1;
	int count_2;

	/* OUTPUTS */
	/* the digital 8 bit output */
	int dig_output;
	/* the analoge output(s) */
	int ana_output_1;
	int ana_output_2;

	/* Value that indicates output is changed */
	short output_changed;

} ueib_handle;

/** CORE **/
ueib_handle *ueib_init(int device);
int ueib_update(ueib_handle *uh);
void ueib_close(ueib_handle *uh);

/** OUTPUT **/
void ueib_set_dig_output(ueib_handle *uh, int bit, int value);
void ueib_set_ana_output(ueib_handle *uh, int output_number, int value);
int ueib_get_dig_output(ueib_handle *uh, int bit);
int ueib_get_ana_output(ueib_handle *uh, int output_number);

/** INPUT **/
int ueib_get_dig_input(ueib_handle *uh, int bit);
int ueib_get_ana_input(ueib_handle *uh, int input_number);
int ueib_get_count_input(ueib_handle *uh, int input_number);
void ueib_reset_count_input(ueib_handle *uh, int input_number);
#endif
